<?php
//Discuz Language library
//Project Name: Discuz Language Translation helping tools
//Author: Fanha Giang a.k.a fanha99
//Email (PayPal Account): fanha99@gmail.com
//License: GPL v2
//You are not allowed to remove these comment!
?>
<?php


function checkdir($dir, $count = 0) {
	global $tlang, $mlang;

	$url = "discuz_langgen.php?mlang=" . $mlang . "&tlang=" . $tlang . "&genfile=";
	
	$st = "";
	for ($i = 0; $i < $count; $i++) { $st = $st . "\t"; }
	echo $st . "Checking dir: " . $dir . "...<br />";
	$d = dir($dir);
	while (false !== ($file = $d->read())) {
		if (is_dir($dir.$file)&&$file!=".."&&$file!=".") checkdir($dir.$file."/", $count + 1);
		else if(is_file($dir.$file) && strpos($file, ".php")!==false) {
		  	if (is_file(str_replace($mlang . '/', $tlang . '/', $dir.$file))) {
			
				echo $st . "Checking file: " . str_replace($mlang . '/', "X/", $dir.$file) . "...  ";
				
				$o = lfile2array($dir.$file);
				
				$t = lfile2array(str_replace($mlang . '/', $tlang . '/', $dir.$file));
				
				echo '<a href="' . $url . $dir.$file . '">'. count($o)." ".count($t)."</a><br />";
				
				$oo = array_keys($o);
				$tt = array_keys($t);
				
				foreach (array_diff($oo, $tt) as $m) {
					echo $st . "\t+ ".$m."<br />";
				}
				foreach (array_diff($tt, $oo) as $m) {
					echo $st . "\t- ".$m."<br />";
				}
							
			} else echo $st . "Missing file ".str_replace($mlang . '/', $tlang . '/', $dir.$file) ."<br />" ;
		}
	}
}

function lfile2array($file) {
	$content = file_get_contents($file);

	$tokens = token_get_all($content);

	// print_r ($tokens);

	$status = array ();
	$level = -1;
	$status[$level] = 0;

	$lang = array ();
	$lang[0] = array();

	foreach ($tokens as $token) {
		if (is_array($token)) {
			if ($token[0] == 366) { // this is the comment, remove them all
			}
			else {
				if ($token[0] == 360) {// array token
					//echo $level . " oas " . $status[$level] ."\n";
					if ($status[$level] == 2) {
						$a = explode("=>", trim($st), 2);
						$lang[$level][trim($a[0])] = array();
						$lang[$level+1] = &$lang[$level][trim($a[0])];
					}
					$level++;
					$status[$level] = 1; // begin the array
					//echo $level . " nas " . $status[$level] ."\n";
				}
				if ($status[$level] == 2) $st = $st . $token[1];
				//echo $token[1];
			}
		}
		else {
			if ($token == '(') {
				//echo $level . " oos " . $status[$level] ."\n";
				if ($status[$level] == 1) $status[$level] = 2; // begin the array
				$st = "";
				//echo $level . " nos " . $status[$level] ."\n";
			}
			else if ($token == ',') {
				if ($status[$level] == 2) 
				{
					$a = explode("=>", trim($st), 2);
					if (count($a) == 2)
					{	
						$lang[$level][trim($a[0])] = trim($a[1]);
					}
				}
				$st = "";
			}
			else if ($token == ')') {
				//echo $level . " ocs " . $status[$level] ."\n";
				if ($status[$level] == 2) 
				{
					$a = explode("=>", trim($st), 2);
					if (count($a) == 2)
					{	
						$lang[$level][trim($a[0])] = trim($a[1]);
					}
					$status[$level] = 0; // end the array
					$level--;
				}
				$st = "";
				//echo $level . " ncs " . $status[$level] ."\n";
			}
			else if ($status[$level] == 2) {
				$st = $st . $token;
			}
			
			// echo $token;
			//if ($token == ',') echo "\n";
		}
	}
	return $lang[0];
}

function output ($o, $t, $level) {
	$st = "";
	for ($i = 0; $i < $level; $i++) { $st = $st . "\t"; }

	$oo = array_keys($o);
	
	foreach ($oo as $m) {
		echo $st . $m . " => ";
		if (is_array($o[$m])){
			echo "array (\n";
			if (isset($t[$m])) output($o[$m], $t[$m], $level + 1);
			else output($o[$m], array(), $level + 1);
			echo $st . "),\n";
		} else {
			if (isset($t[$m])) echo $t[$m]. ",\n";
			else echo $o[$m]. ",\n";				
		}
	}
}

function genfile($file) {
	global $mlang, $tlang;
	
	if(is_file($file)) {
		if (is_file(str_replace($mlang . '/', $tlang . '/', $file))) {
		
			$o = lfile2array($file);
			// print_r($o);
			$t = lfile2array(str_replace($mlang . '/', $tlang . '/', $file));
						
			output ($o, $t, 1);
		}
	}
}

?>